﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
//using Oracle.DataAccess.Client;


public class DataConnect
    {
        private const string CS_REMOTE_READER_NOT_SUPPORT = "ExecuteReader in DataRemote isn't support because it very slow" + Globals.vbCrLf + "Please use ExecuteTable instead of ExecuteReader in DataRemote";
        private static IDataRemoteHelper m_DataRemoteHelper;
        private static String _WorkingConnect, _LoginConnect;
        //---
        private static Dictionary<int, DbTransaction> m_lstTransaction = new Dictionary<int, DbTransaction>();
        private static Random m_rndTransaction = new Random(1);
        private static Dictionary<string, int> m_lstTransID = new Dictionary<string, int>();
        private static DbTransaction Transaction_GetById(int transactionID)
        {
            if (m_lstTransaction.ContainsKey(transactionID))
            {
                return m_lstTransaction[transactionID];
            }
            else
            {
                throw new Exception("Invalid transaction ID");
            }
        }

        private static SqlTransaction Transaction_GetSql(int transactionID)
        {
            if (m_lstTransaction.ContainsKey(transactionID))
            {
                return (SqlTransaction)m_lstTransaction[transactionID];
            }
            else
            {
                throw new Exception("Invalid transaction ID");
            }
        }

        //private static OracleTransaction Transaction_GetOra(int transactionID)
        //{
        //    if (m_lstTransaction.ContainsKey(transactionID))
        //    {
        //        return (OracleTransaction)m_lstTransaction[transactionID];
        //    }
        //    else
        //    {
        //        throw new Exception("Invalid transaction ID");
        //    }
        //}

        private static bool Transaction_IsSql(int transactionID)
        {
            return (m_lstTransaction[transactionID] is SqlTransaction);
        }

        private static DataTable Reader2Table(IDataReader objDataReader)
        {
            DataTable dt = new DataTable();
            dt.Load(objDataReader);
            objDataReader.Close();
            objDataReader.Dispose();
            return dt;
        }

        public static bool Check_Trans(ref string sTransName)
        {
            try
            {
                if (string.IsNullOrEmpty(sTransName) && m_dicThreadTrans.ContainsKey(System.Threading.Thread.CurrentThread.ManagedThreadId))
                {
                    sTransName = m_dicThreadTrans[System.Threading.Thread.CurrentThread.ManagedThreadId];
                }
                return m_lstTransID.ContainsKey(sTransName);
            }
            catch
            {
                return false;
            }
        }
        
        //--------

        public static String LoginConnect
        {
            get
            {
                if (!string.IsNullOrEmpty(_LoginConnect))
                    return _LoginConnect;
                else
                    if (System.Web.HttpRuntime.AppDomainAppId == null)
                        return System.Configuration.ConfigurationManager.AppSettings["LoginConnect"];
                    else
                    {
                        string retVal = Globals.Application_GetConfig("ConnectWorking");
                        if (Globals.String_IsBase64(retVal))
                        {
                            retVal = Globals.Application_GetConfig("ConnectLogin", true);
                        }
                        return retVal;
                    }
            }
            set
            {
                _LoginConnect = value;
            }
        }

        public static String WorkingConnect
        {
            get
            {
                if (!string.IsNullOrEmpty(_WorkingConnect))
                    return _LoginConnect;
                else
                    if (System.Web.HttpRuntime.AppDomainAppId == null)
                        return System.Configuration.ConfigurationManager.AppSettings["WorkingConnect"];
                    else
                    {
                        string retVal = Globals.Application_GetConfig("ConnectWorking");
                        if (Globals.String_IsBase64(retVal))
                        {
                            retVal = Globals.Application_GetConfig("ConnectWorking", true);
                        }
                        return retVal;
                    }
            }
            set
            {
                _LoginConnect = value;
            }
        }

        private static bool IsRemoting(String connStr)
        {
            return connStr.StartsWith("http://") || connStr.StartsWith("tcp://");
        }

        private static bool IsWCF(String connStr)
        {
            return connStr.StartsWith("net.http://") || connStr.StartsWith("net.tcp://");
        }

        public static bool IsRemote
        {
            get
            {
                string connStr = LoginConnect;
                if (IsRemoting(connStr) && m_DataRemoteHelper == null)
                {
                    //m_DataRemoteHelper = new DataWCFHelper();
                    m_DataRemoteHelper = m_DataRemoteHelper;
                }
                else if (IsWCF(connStr) && m_DataRemoteHelper == null)
                {
                    m_DataRemoteHelper = new DataWCFHelper();
                }
                return IsRemoting(connStr) || IsWCF(connStr);
            }
        }

        private static bool IsRemoteEx(String connStr)
        {
            if (IsRemoting(connStr) && m_DataRemoteHelper == null)
            {
                //m_DataRemoteHelper = New DataRemotingHelper
                m_DataRemoteHelper = m_DataRemoteHelper;
            }
            else
            {
                m_DataRemoteHelper = new DataWCFHelper();
                m_DataRemoteHelper.ConnectString = connStr;
            }
            return IsRemoting(connStr) || IsWCF(connStr);
        }

        public static bool Check_Connect(String connStr)
        {
            bool retVal;
            if (IsRemoting(connStr))
            {
                //        'Dim test As New DataRemotingHelper
                //        'retVal = test.Connection_Valid(connStr)
                retVal = true;
            }
            else if (IsWCF(connStr))
            {
                DataWCFHelper test = new DataWCFHelper();
                retVal = test.Connection_Valid(connStr);
            }
            else
            {
                DbConnection conn = null;
                try
                {
                    if (Globals.ConnectString_IsSql(connStr))
                        conn = new SqlConnection(connStr);
                    //else
                     //   conn = new OracleConnection(connStr);
                    //
                    conn.Open();
                    retVal = true;
                }
                catch (Exception ex)
                {
                    retVal = false;
                }
                finally
                {
                    if (conn != null)
                        conn.Close();
                }
            }
            return retVal;
        }

        public static int GetTransId(string sTransName)
        {
            if (!m_lstTransID.ContainsKey(sTransName))
            {
                return 0;
            }
            else
            {
                return m_lstTransID[sTransName];
            }
        }

        public static object GetConnectType(string sTransName)
        {
            if (!m_lstTransID.ContainsKey(sTransName))
            {
                return WorkingConnect;
            }
            else
            {
                return m_lstTransID[sTransName];
            }
        }

        #region "Transaction"
        public static string Begin_Trans()
        {
            string sTransName = "";
            //Check current thread transaction
            if (Check_Trans(ref sTransName))
            {
                return sTransName;
            }
            sTransName = Guid.NewGuid().ToString();
            int transID = 0;
            if (IsRemote)
            {
                transID = m_DataRemoteHelper.Begin_Trans();
            }
            else
            {
                transID = m_rndTransaction.Next();
                while (m_lstTransaction.ContainsKey(transID))
                {
                    transID = m_rndTransaction.Next();
                }
                DbTransaction trans = default(DbTransaction);
                if (Globals.ConnectString_IsSql(WorkingConnect))
                {
                    SqlConnection conn = new SqlConnection(WorkingConnect);
                    conn.Open();
                    trans = conn.BeginTransaction();
                }
                else
                {
                    //OracleConnection conn = new OracleConnection(WorkingConnect);
                    //conn.Open();
                    //trans = conn.BeginTransaction();
                }
                m_lstTransaction.Add(transID, trans);
            }
            //
            m_lstTransID.Add(sTransName, transID);
            //Store in current thread
            if (m_dicThreadTrans.ContainsKey(System.Threading.Thread.CurrentThread.ManagedThreadId))
            {
                m_dicThreadTrans[System.Threading.Thread.CurrentThread.ManagedThreadId] = sTransName;
            }
            else
            {
                m_dicThreadTrans.Add(System.Threading.Thread.CurrentThread.ManagedThreadId, sTransName);
            }
            //
            return sTransName;
        }
        
        private static Dictionary<int, string> m_dicThreadTrans = new Dictionary<int, string>();
        public static bool Rollback_Trans()
        {
            bool retVal = false;
            string sTransName = "";
            //Check current thread transaction
            if (Check_Trans(ref sTransName))
            {
                retVal = Rollback_Trans(sTransName);
                m_dicThreadTrans[System.Threading.Thread.CurrentThread.ManagedThreadId] = "";
            }
            return retVal;
        }

        public static bool Rollback_Trans(string sTransName)
        {
            try
            {
                int _TransactionID = m_lstTransID[sTransName];
                if (IsRemote)
                {
                    m_DataRemoteHelper.Rollback_Trans(_TransactionID);
                }
                else
                {
                    DbTransaction trans = Transaction_GetById(_TransactionID);
                    trans.Rollback();
                    if ((trans.Connection != null))
                        trans.Connection.Close();
                    trans.Dispose();
                    m_lstTransaction.Remove(_TransactionID);
                }
                m_lstTransID.Remove(sTransName);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool Commit_Trans()
        {
            bool retVal = false;
            string sTransName = "";
            //Check current thread transaction
            if (Check_Trans(ref sTransName))
            {
                retVal = Commit_Trans(sTransName);
                m_dicThreadTrans[System.Threading.Thread.CurrentThread.ManagedThreadId] = "";
            }
            return retVal;
        }

        public static bool Commit_Trans(string sTransName)
        {
            try
            {
                int _TransactionID = m_lstTransID[sTransName];
                if (IsRemote)
                {
                    m_DataRemoteHelper.Commit_Trans(_TransactionID);
                }
                else
                {
                    DbTransaction trans = Transaction_GetById(_TransactionID);
                    trans.Commit();
                    if ((trans.Connection != null))
                        trans.Connection.Close();
                    trans.Dispose();
                    m_lstTransaction.Remove(_TransactionID);
                }
                //
                m_lstTransID.Remove(sTransName);
                return true;
            }
            catch
            {
                return false;
            }
        }
        #endregion


        //---------------------------------------------------
        //---------------------------------------------------
        #region " ExecuteNonQuery"

        public static int ExecuteNonQuery(string connectString, CommandType commandType, string commandText)
        {
            if (IsRemoteEx(connectString))
            {
                return m_DataRemoteHelper.ExecuteNonQuery(connectString, commandType, commandText);
            }
            else
            {
                if (Globals.ConnectString_IsSql(connectString))
                {
                    return SqlHelper.ExecuteNonQuery(connectString, commandType, commandText);
                }
                else
                {
                    return -1;// OracleHelper.ExecuteNonQuery(connectString, commandType, commandText);
                }
            }
        }

        public static int ExecuteNonQuery(string connectString, CommandType commandType, string commandText, params object[] commandParameters)
        {
            if (IsRemoteEx(connectString))
            {
                return m_DataRemoteHelper.ExecuteNonQuery(connectString, commandType, commandText, commandParameters);
            }
            else
            {
                if (Globals.ConnectString_IsSql(connectString))
                {

                    return SqlHelper.ExecuteNonQuery(connectString, commandType, commandText, commandParameters);
                }
                else
                {
                    return -1;//OracleHelper.ExecuteNonQuery(connectString, commandType, commandText, commandParameters);
                }
            }
        }

        public static int ExecuteNonQuery(string connectString, string spName, params object[] parameterValues)
        {
            if (IsRemoteEx(connectString))
            {
                return m_DataRemoteHelper.ExecuteNonQuery(connectString, spName, parameterValues);
            }
            else
            {
                if (Globals.ConnectString_IsSql(connectString))
                {
                    return SqlHelper.ExecuteNonQuery(connectString, spName, parameterValues);
                }
                else
                {
                    return -1;// OracleHelper.ExecuteNonQuery(connectString, spName, parameterValues);
                }
            }
        }

        public static int ExecuteNonQuery(string connectString, string spName, object[][] parameterArray)
        {
            if (IsRemoteEx(connectString))
            {
                return m_DataRemoteHelper.ExecuteNonQuery(connectString, spName, parameterArray);
            }
            else
            {
                int iCount = 0;
                foreach (object[] parameterValues in parameterArray)
                {
                    if (Globals.ConnectString_IsSql(connectString))
                    {
                        SqlHelper.ExecuteNonQuery(connectString, spName, parameterValues);
                    }
                    else
                    {
                        //OracleHelper.ExecuteNonQuery(connectString, spName, parameterValues);
                    }
                    iCount += 1;
                }
                return iCount;
            }
        }

        public static int ExecuteNonQuery(int iTransID, CommandType commandType, string commandText)
        {
            if (IsRemote)
            {
                return m_DataRemoteHelper.ExecuteNonQuery(iTransID, commandType, commandText);
            }
            else
            {
                if (Transaction_IsSql(iTransID))
                {
                    return SqlHelper.ExecuteNonQuery(Transaction_GetSql(iTransID), commandType, commandText);
                }
                else
                {
                    return -1;//OracleHelper.ExecuteNonQuery(Transaction_GetOra(iTransID), commandType, commandText);
                }
            }
        }

        public static int ExecuteNonQuery(int iTransID, CommandType commandType, string commandText, params object[] commandParameters)
        {
            if (IsRemote)
            {
                return m_DataRemoteHelper.ExecuteNonQuery(iTransID, commandType, commandText, commandParameters);
            }
            else
            {
                if (Transaction_IsSql(iTransID))
                {
                    return SqlHelper.ExecuteNonQuery(Transaction_GetSql(iTransID), commandType, commandText, commandParameters);
                }
                else
                {
                    return -1;//OracleHelper.ExecuteNonQuery(Transaction_GetOra(iTransID), commandType, commandText, commandParameters);
                }
            }
        }

        public static int ExecuteNonQuery(int iTransID, string spName, params object[] parameterValues)
        {
            if (IsRemote)
            {
                return m_DataRemoteHelper.ExecuteNonQuery(iTransID, spName, parameterValues);
            }
            else
            {
                if (Transaction_IsSql(iTransID))
                {
                    return SqlHelper.ExecuteNonQuery(Transaction_GetSql(iTransID), spName, parameterValues);
                }
                else
                {
                    return -1;//OracleHelper.ExecuteNonQuery(Transaction_GetOra(iTransID), spName, parameterValues);
                }
            }
        }

        public static int ExecuteNonQuery(int iTransID, string spName, object[][] parameterArray)
        {
            if (IsRemote)
            {
                return m_DataRemoteHelper.ExecuteNonQuery(iTransID, spName, parameterArray);
            }
            else
            {
                int iCount = 0;
                foreach (object[] parameterValues in parameterArray)
                {
                    if (Transaction_IsSql(iTransID))
                    {
                        SqlHelper.ExecuteNonQuery(Transaction_GetSql(iTransID), spName, parameterValues);
                    }
                    else
                    {
                        //OracleHelper.ExecuteNonQuery(Transaction_GetOra(iTransID), spName, parameterValues);
                    }
                    iCount += 1;
                }
                return iCount;
            }
        }
        #endregion

        #region " ExecuteDataset"

        public static DataSet ExecuteDataset(string connectString, CommandType commandType, string commandText)
        {
            if (IsRemoteEx(connectString))
            {
                return m_DataRemoteHelper.ExecuteDataset(connectString, commandType, commandText);
            }
            else
            {
                if (Globals.ConnectString_IsSql(connectString))
                {
                    return SqlHelper.ExecuteDataset(connectString, commandType, commandText);
                }
                else
                {
                    return null;//OracleHelper.ExecuteDataset(connectString, commandType, commandText);
                }
            }
        }

        public static DataSet ExecuteDataset(string connectString, CommandType commandType, string commandText, params object[] commandParameters)
        {
            if (IsRemoteEx(connectString))
            {
                return m_DataRemoteHelper.ExecuteDataset(connectString, commandType, commandText, commandParameters);
            }
            else
            {
                if (Globals.ConnectString_IsSql(connectString))
                {
                    return SqlHelper.ExecuteDataset(connectString, commandType, commandText, commandParameters);
                }
                else
                {
                    return null;//OracleHelper.ExecuteDataset(connectString, commandType, commandText, commandParameters);
                }
            }
        }

        public static DataSet ExecuteDataset(string connectString, string spName, params object[] parameterValues)
        {
            if (IsRemoteEx(connectString))
            {
                return m_DataRemoteHelper.ExecuteDataset(connectString, spName, parameterValues);
            }
            else
            {
                if (Globals.ConnectString_IsSql(connectString))
                {
                    return SqlHelper.ExecuteDataset(connectString, spName, parameterValues);
                }
                else
                {
                    return null;//OracleHelper.ExecuteDataset(connectString, spName, parameterValues);
                }
            }
        }

        public static DataSet ExecuteDataset(int iTransID, CommandType commandType, string commandText)
        {
            if (IsRemote)
            {
                return m_DataRemoteHelper.ExecuteDataset(iTransID, commandType, commandText);
            }
            else
            {
                if (Transaction_IsSql(iTransID))
                {
                    return SqlHelper.ExecuteDataset(Transaction_GetSql(iTransID), commandType, commandText);
                }
                else
                {
                    return null;//OracleHelper.ExecuteDataset(Transaction_GetOra(iTransID), commandType, commandText);
                }
            }
        }

        public static DataSet ExecuteDataset(int iTransID, CommandType commandType, string commandText, params object[] commandParameters)
        {
            if (IsRemote)
            {
                return m_DataRemoteHelper.ExecuteDataset(iTransID, commandType, commandText, commandParameters);
            }
            else
            {
                if (Transaction_IsSql(iTransID))
                {
                    return SqlHelper.ExecuteDataset(Transaction_GetSql(iTransID), commandType, commandText, commandParameters);
                }
                else
                {
                    return null;//OracleHelper.ExecuteDataset(Transaction_GetOra(iTransID), commandType, commandText, commandParameters);
                }
            }
        }

        public static DataSet ExecuteDataset(int iTransID, string spName, params object[] parameterValues)
        {
            if (IsRemote)
            {
                return m_DataRemoteHelper.ExecuteDataset(iTransID, spName, parameterValues);
            }
            else
            {
                if (Transaction_IsSql(iTransID))
                {
                    return SqlHelper.ExecuteDataset(Transaction_GetSql(iTransID), spName, parameterValues);
                }
                else
                {
                    return null;//OracleHelper.ExecuteDataset(Transaction_GetOra(iTransID), spName, parameterValues);
                }
            }
        }

        #endregion

        #region " ExecuteTable"

        public static DataTable ExecuteTable(string connectString, CommandType commandType, string commandText)
        {
            if (IsRemoteEx(connectString))
            {
                return m_DataRemoteHelper.ExecuteTable(connectString, commandType, commandText);
            }
            else
            {
                if (Globals.ConnectString_IsSql(connectString))
                {
                    return Reader2Table(SqlHelper.ExecuteReader(connectString, commandType, commandText));
                }
                else
                {
                    return null;//Reader2Table(OracleHelper.ExecuteReader(connectString, commandType, commandText));
                }
            }
        }

        public static DataTable ExecuteTable(string connectString, CommandType commandType, string commandText, params object[] commandParameters)
        {
            if (IsRemoteEx(connectString))
            {
                return m_DataRemoteHelper.ExecuteTable(connectString, commandType, commandText, commandParameters);
            }
            else
            {
                if (Globals.ConnectString_IsSql(connectString))
                {
                    return Reader2Table(SqlHelper.ExecuteReader(connectString, commandType, commandText, commandParameters));
                }
                else
                {
                    return null;//Reader2Table(OracleHelper.ExecuteReader(connectString, commandType, commandText, commandParameters));
                }
            }
        }

        public static DataTable ExecuteTable(string connectString, string spName, params object[] parameterValues)
        {
            if (IsRemoteEx(connectString))
            {
                return m_DataRemoteHelper.ExecuteTable(connectString, spName, parameterValues);
            }
            else
            {
                if (Globals.ConnectString_IsSql(connectString))
                {
                    return Reader2Table(SqlHelper.ExecuteReader(connectString, spName, parameterValues));
                }
                else
                {
                    return null;//Reader2Table(OracleHelper.ExecuteReader(connectString, spName, parameterValues));
                }
            }
        }

        public static DataTable ExecuteTable(int iTransID, CommandType commandType, string commandText)
        {
            if (IsRemote)
            {
                return m_DataRemoteHelper.ExecuteTable(iTransID, commandType, commandText);
            }
            else
            {
                if (Transaction_IsSql(iTransID))
                {
                    return Reader2Table(SqlHelper.ExecuteReader(Transaction_GetSql(iTransID), commandType, commandText));
                }
                else
                {
                    return null;//Reader2Table(OracleHelper.ExecuteReader(Transaction_GetOra(iTransID), commandType, commandText));
                }
            }
        }

        public static DataTable ExecuteTable(int iTransID, CommandType commandType, string commandText, params object[] commandParameters)
        {
            if (IsRemote)
            {
                return m_DataRemoteHelper.ExecuteTable(iTransID, commandType, commandText, commandParameters);
            }
            else
            {
                if (Transaction_IsSql(iTransID))
                {
                    return Reader2Table(SqlHelper.ExecuteReader(Transaction_GetSql(iTransID), commandType, commandText, commandParameters));
                }
                else
                {
                    return null;//Reader2Table(OracleHelper.ExecuteReader(Transaction_GetOra(iTransID), commandType, commandText, commandParameters));
                }
            }
        }

        public static DataTable ExecuteTable(int iTransID, string spName, params object[] parameterValues)
        {
            if (IsRemote)
            {
                return m_DataRemoteHelper.ExecuteTable(iTransID, spName, parameterValues);
            }
            else
            {
                if (Transaction_IsSql(iTransID))
                {
                    return Reader2Table(SqlHelper.ExecuteReader(Transaction_GetSql(iTransID), spName, parameterValues));
                }
                else
                {
                    return null;//Reader2Table(OracleHelper.ExecuteReader(Transaction_GetOra(iTransID), spName, parameterValues));
                }
            }
        }

        #endregion

        #region " ExecuteRow"

        public static DataRowEx ExecuteRow(string connectString, CommandType commandType, string commandText)
        {
            if (IsRemoteEx(connectString))
            {
                return m_DataRemoteHelper.ExecuteRowEx(connectString, commandType, commandText);
            }
            else
            {
                if (Globals.ConnectString_IsSql(connectString))
                {
                    return new DataRowEx(SqlHelper.ExecuteReader(connectString, commandType, commandText));
                }
                else
                {
                    return null;//new DataRowEx(OracleHelper.ExecuteReader(connectString, commandType, commandText));
                }
            }
        }

        public static DataRowEx ExecuteRow(string connectString, CommandType commandType, string commandText, params object[] commandParameters)
        {
            if (IsRemoteEx(connectString))
            {
                return m_DataRemoteHelper.ExecuteRowEx(connectString, commandType, commandText, commandParameters);
            }
            else
            {
                if (Globals.ConnectString_IsSql(connectString))
                {
                    return new DataRowEx(SqlHelper.ExecuteReader(connectString, commandType, commandText, commandParameters));
                }
                else
                {
                    return null;//new DataRowEx(OracleHelper.ExecuteReader(connectString, commandType, commandText, commandParameters));
                }
            }
        }

        public static DataRowEx ExecuteRow(string connectString, string spName, params object[] parameterValues)
        {
            if (IsRemoteEx(connectString))
            {
                return m_DataRemoteHelper.ExecuteRowEx(connectString, spName, parameterValues);
            }
            else
            {
                if (Globals.ConnectString_IsSql(connectString))
                {
                    return new DataRowEx(SqlHelper.ExecuteReader(connectString, spName, parameterValues));
                }
                else
                {
                    return null;//new DataRowEx(OracleHelper.ExecuteReader(connectString, spName, parameterValues));
                }
            }
        }

        public static DataRowEx ExecuteRow(int iTransID, CommandType commandType, string commandText)
        {
            if (IsRemote)
            {
                return m_DataRemoteHelper.ExecuteRowEx(iTransID, commandType, commandText);
            }
            else
            {
                if (Transaction_IsSql(iTransID))
                {
                    return new DataRowEx(SqlHelper.ExecuteReader(Transaction_GetSql(iTransID), commandType, commandText));
                }
                else
                {
                    return null;//new DataRowEx(OracleHelper.ExecuteReader(Transaction_GetOra(iTransID), commandType, commandText));
                }
            }
        }

        public static DataRowEx ExecuteRow(int iTransID, CommandType commandType, string commandText, params object[] commandParameters)
        {
            if (IsRemote)
            {
                return m_DataRemoteHelper.ExecuteRowEx(iTransID, commandType, commandText, commandParameters);
            }
            else
            {
                if (Transaction_IsSql(iTransID))
                {
                    return new DataRowEx(SqlHelper.ExecuteReader(Transaction_GetSql(iTransID), commandType, commandText, commandParameters));
                }
                else
                {
                    return null;//new DataRowEx(OracleHelper.ExecuteReader(Transaction_GetOra(iTransID), commandType, commandText, commandParameters));
                }
            }
        }

        public static DataRowEx ExecuteRow(int iTransID, string spName, params object[] parameterValues)
        {
            if (IsRemote)
            {
                return m_DataRemoteHelper.ExecuteRowEx(iTransID, spName, parameterValues);
            }
            else
            {
                if (Transaction_IsSql(iTransID))
                {
                    return new DataRowEx(SqlHelper.ExecuteReader(Transaction_GetSql(iTransID), spName, parameterValues));
                }
                else
                {
                    return null;//new DataRowEx(OracleHelper.ExecuteReader(Transaction_GetOra(iTransID), spName, parameterValues));
                }
            }
        }

        #endregion

        #region " ExecuteReader"

        public static IDataReader ExecuteReader(string connectString, CommandType commandType, string commandText)
        {
            if (IsRemoteEx(connectString))
            {
                throw new Exception(CS_REMOTE_READER_NOT_SUPPORT);
            }
            else
            {
                if (Globals.ConnectString_IsSql(connectString))
                {
                    return SqlHelper.ExecuteReader(connectString, commandType, commandText);
                }
                else
                {
                    return null;//OracleHelper.ExecuteReader(connectString, commandType, commandText);
                }
            }
        }

        public static IDataReader ExecuteReader(string connectString, CommandType commandType, string commandText, params object[] commandParameters)
        {
            if (IsRemoteEx(connectString))
            {
                throw new Exception(CS_REMOTE_READER_NOT_SUPPORT);
            }
            else
            {
                if (Globals.ConnectString_IsSql(connectString))
                {
                    return SqlHelper.ExecuteReader(connectString, commandType, commandText, commandParameters);
                }
                else
                {
                    return null;//OracleHelper.ExecuteReader(connectString, commandType, commandText, commandParameters);
                }
            }
        }

        public static IDataReader ExecuteReader(string connectString, string spName, params object[] parameterValues)
        {
            if (IsRemoteEx(connectString))
            {
                throw new Exception(CS_REMOTE_READER_NOT_SUPPORT);
            }
            else
            {
                if (Globals.ConnectString_IsSql(connectString))
                {
                    return SqlHelper.ExecuteReader(connectString, spName, parameterValues);
                }
                else
                {
                    return null;//OracleHelper.ExecuteReader(connectString, spName, parameterValues);
                }
            }
        }

        public static IDataReader ExecuteReader(int iTransID, CommandType commandType, string commandText)
        {
            if (IsRemote)
            {
                throw new Exception(CS_REMOTE_READER_NOT_SUPPORT);
            }
            else
            {
                if (Transaction_IsSql(iTransID))
                {
                    return SqlHelper.ExecuteReader(Transaction_GetSql(iTransID), commandType, commandText);
                }
                else
                {
                    return null;//OracleHelper.ExecuteReader(Transaction_GetOra(iTransID), commandType, commandText);
                }
            }
        }

        public static IDataReader ExecuteReader(int iTransID, CommandType commandType, string commandText, params object[] commandParameters)
        {
            if (IsRemote)
            {
                throw new Exception(CS_REMOTE_READER_NOT_SUPPORT);
            }
            else
            {
                if (Transaction_IsSql(iTransID))
                {
                    return SqlHelper.ExecuteReader(Transaction_GetSql(iTransID), commandType, commandText, commandParameters);
                }
                else
                {
                    return null;//OracleHelper.ExecuteReader(Transaction_GetOra(iTransID), commandType, commandText, commandParameters);
                }
            }
        }

        public static IDataReader ExecuteReader(int iTransID, string spName, params object[] parameterValues)
        {
            if (IsRemote)
            {
                throw new Exception(CS_REMOTE_READER_NOT_SUPPORT);
            }
            else
            {
                if (Transaction_IsSql(iTransID))
                {
                    return SqlHelper.ExecuteReader(Transaction_GetSql(iTransID), spName, parameterValues);
                }
                else
                {
                    return null;//OracleHelper.ExecuteReader(Transaction_GetOra(iTransID), spName, parameterValues);
                }
            }
        }

        #endregion

        #region " ExecuteScalar"

        public static object ExecuteScalar(string connectString, CommandType commandType, string commandText)
        {
            if (IsRemoteEx(connectString))
            {
                return m_DataRemoteHelper.ExecuteScalar(connectString, commandType, commandText);
            }
            else
            {
                if (Globals.ConnectString_IsSql(connectString))
                {
                    return SqlHelper.ExecuteScalar(connectString, commandType, commandText);
                }
                else
                {
                    return null;//OracleHelper.ExecuteScalar(connectString, commandType, commandText);
                }
            }
        }

        public static object ExecuteScalar(string connectString, CommandType commandType, string commandText, params object[] commandParameters)
        {
            if (IsRemoteEx(connectString))
            {
                return m_DataRemoteHelper.ExecuteScalar(connectString, commandType, commandText, commandParameters);
            }
            else
            {
                if (Globals.ConnectString_IsSql(connectString))
                {
                    return SqlHelper.ExecuteScalar(connectString, commandType, commandText, commandParameters);
                }
                else
                {
                    return null;//OracleHelper.ExecuteScalar(connectString, commandType, commandText, commandParameters);
                }
            }
        }

        public static object ExecuteScalar(string connectString, string spName, params object[] parameterValues)
        {
            if (IsRemoteEx(connectString))
            {
                return m_DataRemoteHelper.ExecuteScalar(connectString, spName, parameterValues);
            }
            else
            {
                if (Globals.ConnectString_IsSql(connectString))
                {
                    return SqlHelper.ExecuteScalar(connectString, spName, parameterValues);
                }
                else
                {
                    return null;//OracleHelper.ExecuteScalar(connectString, spName, parameterValues);
                }
            }
        }

        public static object ExecuteScalar(string connectString, string spName, object[][] parameterArray)
        {
            if (IsRemoteEx(connectString))
            {
                return m_DataRemoteHelper.ExecuteScalar(connectString, spName, parameterArray);
            }
            else
            {
                object[] arrReturn = new object[parameterArray.Length];
                //Array.CreateInstance(typeof(object), parameterArray.Length);
                for (int i = 0; i <= parameterArray.Length - 1; i++)
                {
                    if (Globals.ConnectString_IsSql(connectString))
                    {
                        arrReturn[i] = SqlHelper.ExecuteScalar(connectString, spName, parameterArray[i]);
                    }
                    else
                    {
                        //arrReturn[i] = OracleHelper.ExecuteScalar(connectString, spName, parameterArray[i]);
                    }
                }
                return arrReturn;
            }
        }

        public static object ExecuteScalar(int iTransID, CommandType commandType, string commandText)
        {
            if (IsRemote)
            {
                return m_DataRemoteHelper.ExecuteScalar(iTransID, commandType, commandText);
            }
            else
            {
                if (Transaction_IsSql(iTransID))
                {
                    return SqlHelper.ExecuteScalar(Transaction_GetSql(iTransID), commandType, commandText);
                }
                else
                {
                    return null;//OracleHelper.ExecuteScalar(Transaction_GetOra(iTransID), commandType, commandText);
                }
            }
        }

        public static object ExecuteScalar(int iTransID, CommandType commandType, string commandText, params object[] commandParameters)
        {
            if (IsRemote)
            {
                return m_DataRemoteHelper.ExecuteScalar(iTransID, commandType, commandText, commandParameters);
            }
            else
            {
                if (Transaction_IsSql(iTransID))
                {
                    return SqlHelper.ExecuteScalar(Transaction_GetSql(iTransID), commandType, commandText, commandParameters);
                }
                else
                {
                    return null;//OracleHelper.ExecuteScalar(Transaction_GetOra(iTransID), commandType, commandText, commandParameters);
                }
            }
        }

        public static object ExecuteScalar(int iTransID, string spName, params object[] parameterValues)
        {
            if (IsRemote)
            {
                return m_DataRemoteHelper.ExecuteScalar(iTransID, spName, parameterValues);
            }
            else
            {
                if (Transaction_IsSql(iTransID))
                {
                    return SqlHelper.ExecuteScalar(Transaction_GetSql(iTransID), spName, parameterValues);
                }
                else
                {
                    return null;//OracleHelper.ExecuteScalar(Transaction_GetOra(iTransID), spName, parameterValues);
                }
            }
        }

        public static object ExecuteScalar(int iTransID, string spName, object[][] parameterArray)
        {
            if (IsRemote)
            {
                return m_DataRemoteHelper.ExecuteScalar(iTransID, spName, parameterArray);
            }
            else
            {
                object[] arrReturn = new object[parameterArray.Length];
                for (int i = 0; i <= parameterArray.Length - 1; i++)
                {
                    if (Transaction_IsSql(iTransID))
                    {
                        arrReturn[i] = SqlHelper.ExecuteScalar(Transaction_GetSql(iTransID), spName, parameterArray[i]);
                    }
                    else
                    {
                        //arrReturn[i] = OracleHelper.ExecuteScalar(Transaction_GetOra(iTransID), spName, parameterArray[i]);
                    }
                }
                return arrReturn;
            }
        }

        #endregion

        internal static DataRowEx ExecuteRowEx(int p, string spName, object[] parameterValues)
        {
            throw new NotImplementedException();
        }
    }
